package io.jboot.fly.directive;

import com.jfinal.template.Env;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;
import io.jboot.fly.model.UserAction;
import io.jboot.fly.service.PostService;
import io.jboot.fly.service.UserActionService;
import io.jboot.fly.service.UserAttachmentService;
import io.jboot.web.directive.annotation.JFinalDirective;
import io.jboot.web.directive.base.JbootDirectiveBase;

import javax.inject.Inject;
import java.util.List;

@JFinalDirective("MyUploadStatistics")
public class MyUploadStatistics extends JbootDirectiveBase {

    @Inject
    UserActionService userActionService;
    @Inject
    PostService postService;
    @Inject
    UserAttachmentService userAttachmentService;

    @Override
    public void onRender(Env env, Scope scope, Writer writer) {
        String  userId = getParam("userId",scope);
        List<UserAction> ua =userActionService.findByUserIdAndAction(userId,UserAction.ACTION_UPLOAD);
        int myUploadsum = postService.getPostForUploadCount(userId);
        int myDownloadCount = ua!=null?ua.size():0;
        int myFileDownLoadedCount = userAttachmentService.myFileDownLoadedCount(userId);
        //myFileDownloadSum
        scope.setLocal("myUploadsum", myUploadsum);
        scope.setLocal("myDownloadCount", myDownloadCount);
        scope.setLocal("myFileDownLoadedCount", myFileDownLoadedCount);
        renderBody(env, scope, writer);
    }

    @Override
    public boolean hasEnd() {
        return true;
    }
}
